<template>
  <client-only v-if="!isDiv">
    <svg :class="['svg-icon', svgClass]"
      aria-hidden="true">
      <use :xlink:href="svgName"></use>
    </svg>
  </client-only>
  <div v-else
    :class="[className]">
    <svg :class="['svg-icon', svgClass]"
      aria-hidden="true">
      <use :xlink:href="svgName"></use>
    </svg>
  </div>
</template>
<script>
export default {
  name: 'svgIcon',
  props: {
    iconName: {
      type: String,
      default: '',
    },
    className: {
      type: String,
      default: '',
    },
    isDiv: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    isExternal() {
      return /^(https?:|mailto:|tel:)/.test(this.iconClass)
    },
    svgName() {
      return `#icon-${this.iconName}`
    },
    svgClass() {
      if (this.className) {
        return this.className
      }
    },
    // 返回url请求位置
    styleExternalIcon() {
      return {
        mask: `url(${this.iconClass}) no-repeat 50% 50%`,
        '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`,
      }
    },
  },
}
</script>
<style lang="scss" scoped>
svg:not(:root) {
  overflow: hidden;
}
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  stroke: currentColor;
  overflow: hidden;
}
.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}
</style>
